#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'builder_starter'
require 'optparse'
require 'fileutils'

# HACK: Options copy/pasted from mongrel.rb in order to add custom options.
# (this section, therefore, may need to be manually updated when upgrading to a new Rails version)
ARGV.clone.options do |opts|
  opts.on("-p", "--port=port", Integer, "Runs Rails on the specified port.", "Default: 3333") {}
  opts.on("-b", "--binding=ip", String, "Binds Rails to the specified ip.", "Default: 0.0.0.0") {}
  opts.on("-d", "--daemon", "Make server run as a Daemon.") {}
  opts.on("-e", "--environment=name", String,
          "Specifies the environment to run this server under (test/development/production).",
          "Default: production") {}
  opts.on("-w", "--without-builders", "Run without starting the project builders.") { BuilderStarter.run_builders_at_startup = false }
  opts.on("--trace", "Start builders in verbose mode."){ $VERBOSE_MODE = true }

  opts.separator ""

  opts.on("-h", "--help", "Show this help message.") { puts opts; exit }

  opts.parse!
end

# Remove custom options so that they are not interpreted by 'commands/server'   
ARGV.delete("-w")
ARGV.delete("--without-builders")
ARGV.delete("--trace")
    
# change default port from Rails' usual 3000 to 3333, so that it doesn't clash with other Rails apps 
# running on the same box.
if (ARGV & ['-p', '--port']).empty?
  ARGV << '-p' << '3333'
end

# change default environment to production
if (ARGV & ['-e', '--environment']).empty?
  ARGV << '-e' << 'production'
end

require 'commands/server'
